package cz.semjob.linguistic.reader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.semjob.linguistic.data.LinguisticSentence;
import cz.semjob.linguistic.data.LinguisticWord;
import cz.semjob.linguistic.reader.attribute.value.linguistic.ParserAttributeValue;
import cz.semjob.linguistic.reader.attribute.value.linguistic.PartOfSpeechAttributeValue;
import cz.semjob.linguistic.reader.factory.IParserFactory;
import cz.semjob.linguistic.reader.factory.IPartOfSpeechFactory;

@Component
public class CoNNLXReader implements ILinguisticReader {

	private IPartOfSpeechFactory posFactory;

	private IParserFactory parserFactory;

	@Autowired
	public CoNNLXReader(IPartOfSpeechFactory posFactory,
			IParserFactory parserFactory) {
		this.posFactory = posFactory;
		this.parserFactory = parserFactory;
	};

	public List<LinguisticSentence> readDocument(File sourceFile)
			throws LinguisticFormatProcessingException, IOException {

		List<String> lines = FileUtils.readLines(sourceFile);
		List<LinguisticSentence> sentences = new ArrayList<LinguisticSentence>();

		List<String> sentenceLines = new ArrayList<String>();
		for (String line : lines) {
			if (line.isEmpty() || lines.lastIndexOf(line) == lines.size() - 1) {
				LinguisticSentence linguisticSentence = processSentence(sentenceLines);
				sentences.add(linguisticSentence);
				sentenceLines.clear();
			} else {
				sentenceLines.add(line);
			}

		}

		return sentences;
	}

//	public LanguageConfig getLanguageConfig() {
//		return new LanguageConfig(
//				parserFactory.getLanguageConfigRelationshipAttribute(),
//				posFactory.getLanguageConfigPartOfSpeechAttribute(),
//				posFactory.getLanguageConfigNumberAttribute(),
//				posFactory.getLanguageConfigCaseAttribute(),
//				posFactory.getGenderAttribute());
//	}

	private LinguisticSentence processSentence(List<String> lines)
			throws LinguisticFormatProcessingException {
		LinguisticSentence sentence = new LinguisticSentence();
		for (String line : lines) {
			LinguisticWord linguisticWord = processWord(line);
			sentence.addWord(linguisticWord);
		}
		return sentence;
	}

	private LinguisticWord processWord(String line)
			throws LinguisticFormatProcessingException {

		LinguisticWord word = new LinguisticWord();

		String[] columns = line.split("\t");

		if (columns.length < 2) {
			throw new LinguisticFormatProcessingException(
					"Wrong input line, at least 2 columns are needed: " + line);
		}

		int index = Integer.valueOf(columns[0]);
		word.setId(index);

		String wordString = columns[1];
		word.setWord(wordString);

		if (columns.length > 2) {
			String lemma = columns[2];
			word.setLemma(lemma);
		}

		if (columns.length > 4) {
			PartOfSpeechAttributeValue posAttribute = posFactory
					.createAttribute(columns[4]);
			word.setPosAttribute(posAttribute);
		}

		if (columns.length > 7) {
			ParserAttributeValue parserAttribute = parserFactory
					.createAttribute(columns[6], columns[7]);
			word.setParserAttribute(parserAttribute);
		}

		return word;
	}
}
